#include "BattleGrid.h"
#include "Pawn.h"
#include "JokeType.h"

BattleGrid::BattleGrid()
	:m_pawn(0)
	,m_mapTileX(-1)
	,m_mapTileY(-1)
	,m_battleFieldIndexX(-1)
	,m_battleFieldIndexY(-1)
	,m_horizontalLock(false)
	,m_verticalLock(false)
	,m_newLock(true)
{

}

void BattleGrid::setPawn( Pawn* p )
{
	m_pawn = p;
}

void BattleGrid::setMapTile( int x,int y )
{
	m_mapTileX = x;
	m_mapTileY = y;
}

void BattleGrid::setBattleFieldIndex(int x,int y)
{
	m_battleFieldIndexX = x;
	m_battleFieldIndexY = y;
}

bool BattleGrid::isPawnActionRun(int tag) const
{
	if (m_pawn)
	{
		return m_pawn->isActionRun(tag);
	}

	return false;
}

BattleGrid& BattleGrid::clone(const BattleGrid& grid)
{
	m_battleFieldIndexX = grid.getBattleFieldIndexX();
	m_battleFieldIndexY = grid.getBattleFieldIndexY();
	m_mapTileX = grid.getMapTileX();
	m_mapTileY = grid.getMapTileY();
	cloneLogicInfo(grid);

	return *this;
}

BattleGrid& BattleGrid::cloneLogicInfo(const BattleGrid& grid)
{
	m_pawn = grid.getPawn();
	m_newLock = grid.isNewLock();
	m_horizontalLock = grid.isHorizontalLock();
	m_verticalLock = grid.isVerticalLock();

	return *this;
}

void BattleGrid::killPawn()
{
	CCLOG("BattleGrid::killPawn");
	if (m_pawn)
	{
		m_pawn->output();
		delete m_pawn;
		m_pawn = NULL;
	}
	
	clear();
}

void BattleGrid::clear()
{
	m_pawn = NULL;
	m_horizontalLock = false;
	m_verticalLock = false;
	m_newLock = true;
}

void BattleGrid::outputInfo()
{
	CCLOG("BattleGrid[%d][%d] : MapTile[%d][%d] horizontalLock:%d,verticalLock:%d,newLock:%d,hadPawn:%d",m_battleFieldIndexX,m_battleFieldIndexY,m_mapTileX,m_mapTileY,m_horizontalLock,m_verticalLock,m_newLock,m_pawn);
	if (m_pawn)
	{
		m_pawn->output();
	}
}

void BattleGrid::setNewLock( bool v )
{
	m_newLock = v;
	if (isHorizontalLock() == false && isVerticalLock() == false && isNewLock() == false)
	{
		assert(0);
	}
}
